package bf.program;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class Loop extends CompiableCode {

//	public static final int COMPILE_MODE_NONE = 0;
	public static final int COMPILE_MODE_PRE = 1;
	public static final int COMPILE_MODE_JIT = 2;

	public static final int COMPILE_MODE = COMPILE_MODE_PRE;

//	@Override
//	public void compile() {
//		codeBlock.compile();
//	}

	CodeBlock codeBlock;
	
	public Loop(CodeBlock prog) {
		codeBlock = prog;
	}

	@Override
	public void genCode(MethodVisitor mv) {
		Label l5 = new Label();
		mv.visitJumpInsn(GOTO, l5);
		Label l6 = new Label();
		mv.visitLabel(l6);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		
//		genCall(mv, codeBlock.getCompiled());
		codeBlock.genCode(mv);
		
		mv.visitLabel(l5);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKEVIRTUAL, "bf/program/State", "CV", "()B");
		mv.visitJumpInsn(IFNE, l6);
	}

	@Override
	public void iexecute(State state) {
		while(state.CV() != 0) {
			codeBlock.execute(state);
		}
	}

}
